define(function(require, exports, module) {
  var $ = require("$");
  var loader = require("./loader/loader");
  require("bootstrap_commonrequire");
  require("moment");
  require("bootstrap-daterangepicker");
  require("daterangepicker.css");

  /* 监听窗口大小变化，增强jquery的resize方法，可以激情dom元素 */
  require('js/core/jquery/src/jquery_resize');

  /* 添加ztree引用 */
  require("zTreeStyle.css");
  require("ztreeCore");
  require("ztreeExcheck");

  require('select2.css');
  require('select2');
  var store = require('store');

  exports.init = function(param) {

    $(document).ready(function() {
      // 加载自定义模板-条件区域
      var isSystemAdmin = (store.get("userInfo").userType == -1);
      var tplattrs = {};
      tplattrs.isSystemAdmin = isSystemAdmin;

      var template = require("./remind.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template,
      });
      new Component({
        attrs : tplattrs,
        renderTo : queryAreaId
      });

      // 获取查询条件
      function getCondition() {

        var code = $('#code').val();

        var queryStr = "";
        if (code != '' && code != null) {
          queryStr += ("code like '%" + code + "%';");
        }
        return queryStr;
      }
      // 加载表单模板和数据
      var Table = require("table");
      new Table({
        attrs : {
          id : "datatableid",
          isHovered : false,
          isStriped : true,
          isBordered : false,
        },
        renderTo : param.divWrapperId

      });

      var dtUtil = require('datatableutil');

      var datatable = $('#datatableid').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl, // 数据源
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          // 拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if(json.flag == '000000'){
                dtUtil.successCallback(json, aoData, fnCallback);                    
              } else {
                oauth2Relogin();                    
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "业务编码",
          "bSortable" : false,
          "mData" : "code",
          "sWidth" : "35%"
        },{
            "sTitle" : "状态",
            "bSortable" : false,
            "sWidth" : "20%",
            "mData" : "state",
            "mRender" : function(data, state, full)
            {
              if (data == 0)
                return "停用";
              else if(data == 1)
                return "启用";
            }
          } ,
            {
              "sTitle" : "创建时间",
              "bSortable" : false,
              "sWidth" : "30%",
              "mData" : "create_time"
            } ,{
                "sTitle" : "操作",
                "bSortable" : false,
                "sWidth" : "15%",
                "mData" : null,
                "mRender" : function(data, state, full)
                {
                  var ret = '<div class="btn-group" style="margin-top: 8px;">';
                      ret += '<a href="javascript:void(0);" class="btn icon-rocket contrast" title="启用/停用"></a>';
                  return ret += '</div>';
                }
              }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });
      //启用/停用
      $("#data_table tbody").on("click", "tr>td>div>a.icon-rocket", function(e)
      {
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = datatable.fnGetData(rowNode);
        
        loader.loading("正在提交，请稍等……");
        $.ajax({
          url : "/remindManage/updateState.do",//GLOBAL.URL.ADMIN_INSERTLICENSECARRIER,
          dataType : "json",
          data : "code=" + rowData.code,
          type : "POST",
          success : function(json) {
            if(json.flag == '000000'){
              datatable.fnDraw();
              loader.noloading();                   
            } else {
              oauth2Relogin();                    
            }
          },
          error : function(s) {
            loader.noloading();
            bootbox.alertTimeout("网络请求失败，请重试！");
          }
        });
      });
      $('#btnquery').click(function() {
        datatable.fnDraw();
        return false;
      });

    });

  };
});
